﻿using Microsoft.AspNetCore.Mvc;
using System.Reflection.Emit;
using yunbo.Model.BI;
using yunbo.Model.BIP;
using yunbo.Model.BIQI;
using yunbo.Repositorys.BIQIRepositorys;
using yunbo.Repositorys.BIRepositorys;
using yunbo.Repositorys.FPMRepositorys;

namespace yunbo.Controllers.BIQIControllers
{
	[Route("api/[controller]")]
	[ApiController]
	public class QMS_ZJXMController : Controller
	{
		QMS_ZJXMRepository qMS_ZJXMRepository = new QMS_ZJXMRepository();


		/// <summary>
		/// 获取质检项目
		/// </summary>
		/// <param name="state"></param>
		/// <param name="str"></param>
		/// <returns></returns>
		[HttpGet("GetQMS_ZJXM")]
		public IActionResult GetQMS_ZJXM(int state, string? str)
		{
			List<QMS_ZJXM> zjxm = qMS_ZJXMRepository.GetQMS_ZJXM(state, str);
			return Ok(zjxm);
		}


		/// <summary>
		/// 绑定质检项目下拉框
		/// </summary>
		/// <returns></returns>
		[HttpGet("GetOneQMS_ZJXM")]
		public IActionResult GetOneQMS_ZJXM()
		{
			List<QMS_ZJXM> zjxm = qMS_ZJXMRepository.GetOneQMS_ZJXM();
			return Ok(zjxm);
		}


		[HttpGet("GetZJXMs")]
		public IActionResult GetZJXMs(string? codes)
		{
			List<QMS_ZJXM> zjxm = qMS_ZJXMRepository.GetZJXMs(codes);
			return Ok(zjxm);
		}


		[HttpGet("GetQMS_ZJXMzjxm_code")]
		public IActionResult GetQMS_ZJXMzjxm_code(string zjxm_code)
		{
			QMS_ZJXM quans = qMS_ZJXMRepository.GetQMS_ZJXMzjxm_code(zjxm_code);
			return Ok(quans);
		}

		/// <summary>
		/// 改变状态
		/// </summary>
		/// <param name="id"></param>
		/// <param name="state"></param>
		/// <returns></returns>
		[HttpPut("StateChange")]
		public IActionResult StateChange(int id, int state)
		{
			bool res = qMS_ZJXMRepository.StateChange(id, state);
			return Ok(res);
		}


		/// <summary>
		/// 新增
		/// </summary>
		/// <param name="zjxm"></param>
		/// <returns></returns>
		[HttpPost("AddQMS_ZJXM")]
		public IActionResult AddQMS_ZJXM(QMS_ZJXM zjxm)
		{
			// 数据验证（可选）
			if (string.IsNullOrEmpty(zjxm.zjxm_code)
				|| string.IsNullOrEmpty(zjxm.zjxm_name)
				|| zjxm.zjxm_sx <= 0
				|| string.IsNullOrEmpty(zjxm.zjxm_jybz)
				|| string.IsNullOrEmpty(zjxm.zjxm_max)
				|| string.IsNullOrEmpty(zjxm.zjxm_min)
				|| zjxm.zjxm_dw <= 0
				)
			{
				return BadRequest("Invalid dictionary data."); // 如果数据无效，返回 400 Bad Request
			}

			bool res = qMS_ZJXMRepository.AddQMS_ZJXM(zjxm);
			return Ok(res);
		}


		/// <summary>
		/// 修改
		/// </summary>
		/// <param name="zjxm"></param>
		/// <returns></returns>
		[HttpPut("UpdateQMS_ZJXM")]
		public IActionResult UpdateQMS_ZJXM(QMS_ZJXM zjxm)
		{
			// 数据验证（可选）
			if (string.IsNullOrEmpty(zjxm.zjxm_name)
				|| zjxm.zjxm_sx <= 0
				|| string.IsNullOrEmpty(zjxm.zjxm_jybz)
				|| string.IsNullOrEmpty(zjxm.zjxm_max)
				|| string.IsNullOrEmpty(zjxm.zjxm_min)
				|| zjxm.zjxm_dw <= 0
				|| zjxm.zjxm_id <= 0
				)
			{
				return BadRequest("Invalid dictionary data."); // 如果数据无效，返回 400 Bad Request
			}

			bool res = qMS_ZJXMRepository.UpdateQMS_ZJXM(zjxm);
			return Ok(res);
		}


		/// <summary>
		/// 删除
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		[HttpPut("DelQMS_ZJXM")]
		public IActionResult DelDW(int id)
		{
			bool res = qMS_ZJXMRepository.DelQMS_ZJXM(id);
			return Ok(res);
		}


		/// <summary>
		/// 批量删除
		/// </summary>
		/// <param name="ids"></param>
		/// <returns></returns>
		[HttpPut("DelQMS_ZJXMs")]
		public IActionResult DelDWs(int[] ids)
		{
			bool res = qMS_ZJXMRepository.DelQMS_ZJXMs(ids);
			return Ok(res);
		}

	}
}
